import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'
import Users from "@/views/user/Users.vue" //用户列表
import UserAdd from "@/views/user/Add.vue" //用户添加
import UserEdit from "@/views/user/Edit.vue" //用户编辑
import Rights from "@/views/power/Rights.vue" //权限列表
import Roles from "@/views/power/Roles.vue" //角色列表
import Report from "@/views/report/Report.vue" //报表
import Order from "@/views/order/Order.vue" //订单列表
import categories from "@/views/goods/Cate.vue" //商品分类
import Params from "@/views/goods/Params.vue" //分类参数
import List from "@/views/goods/List.vue" //商品列表
import Add from "@/views/goods/Add.vue" //商品添加
import Edit from "@/views/goods/Edit.vue" //商品编辑


Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login' // 重定向
  },
  {
    path:'/digui',
    name:'digui',
    component:()=>import('../views/user/DiGui.vue')
  },
  {
    path:'/login',
    name:'login',
    component:Login
  },
  {
    path:'/main',
    name:'main',
    component:Main,
    redirect:'/wel',
    children:[
      {
        path:'/wel',
        name:'wel',
        component:()=>import('../views/Welcome.vue')
      },

      {
        path:'/users',
        name:'users',
        component:Users
      },
      {
        path:'/useradd',
        name:'useradd',
        component:UserAdd
      },
      {
        path:'/useredit',
        name:'useredit',
        component:UserEdit
      },

      {
        path:'/rights',
        name:'rights',
        component:Rights
      },
      {
        path:'/roles',
        name:'roles',
        component:Roles
      },

      {
        path:'/reports',
        name:'reports',
        component:Report
      },

      {
        path:'/orders',
        name:'orders',
        component:Order
      },

      {
        path:'/categories',
        name:'categories',
        component:categories
      },
      {
        path:'/params',
        name:'params',
        component:Params
      },
      {
        path:'/goods',
        name:'goods',
        component:List
      },
      {
        path:'/add',
        name:'add',
        component:Add
      },
      {
        path:'/edit/:id',
        name:'edit',
        component:Edit
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前置导航守卫，判断是否登录
router.beforeEach((to,from,next)=>{
  if(to.path!='/login'&&to.path!='/'){
    const token = sessionStorage.getItem('token');
    if(!token){
      // 不要用router.push(),因为这样会导致死循环,必须用next()
      next('/login')
    }
  }
  next()
})

export default router
